//
//  AppDelegate.m
//  ScreenRecord_ios_oc
//
//  Created by 51talk on 2021/5/11.
//

#import "AppDelegate.h"
//#import <BackgroundTasks/BackgroundTasks.h>
#import "appRunInBackground.h"


@interface AppDelegate ()

//@property (nonatomic, assign) UIBackgroundTaskIdentifier backgroundTaskIdentifier;

@end

static NSString *const kBgTaskName = @"com.qishare.ios.wyw.QiAppRunInBackground";

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [self registerBgTask];
    return YES;
}

- (void)registerBgTask {
//
//    if (@available(iOS 13.0, *)) {
//        BOOL registerFlag = [[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:kRefreshTaskId usingQueue:nil launchHandler:^(__kindof BGTask * _Nonnull task) {
//            [self handleAppRefresh:task];
//        }];
//        if (registerFlag) {
//            NSLog(@"注册成功");
//        } else {
//            NSLog(@"注册失败");
//        }
//    } else {
//        // Fallback on earlier versions
//    }
//
//    if (@available(iOS 13.0, *)) {
//        [[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:kCleanTaskId usingQueue:nil launchHandler:^(__kindof BGTask * _Nonnull task) {
//            [self handleAppRefresh:task];
//        }];
//    } else {
//        // Fallback on earlier versions
//    }
}

//
//- (void)handleAppRefresh:(BGAppRefreshTask *)appRefreshTask  API_AVAILABLE(ios(13.0)){
//}

- (void)applicationWillEnterForeground:(UIApplication *)application {

    NSLog(@"%s：应用将进入前台WillEnterForeground", __FUNCTION__);
//    [[UIApplication sharedApplication] endBackgroundTask: self.backgroundTaskIdentifier];
//    [[AppRunInBackground getInstance] stopBGRun];
}


- (void)applicationDidEnterBackground:(UIApplication *)application {

    NSLog(@"%s：应用进入后台DidEnterBackground", __FUNCTION__);
//    [[AppRunInBackground getInstance] startBGRun];
//    self.backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithName:kBgTaskName expirationHandler:^{

//       if (self.backgroundTaskIdentifier != UIBackgroundTaskInvalid) {
//           [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier];
//           self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
//       }
//    }];
}

//
//
//#pragma mark - UISceneSession lifecycle
//
//
//- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
//    // Called when a new scene session is being created.
//    // Use this method to select a configuration to create the new scene with.
//    return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
//}
//
//
//- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
//    // Called when the user discards a scene session.
//    // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
//    // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
//}
//

@end
